package tests;

import heap.HFBufMgrException;
import heap.HFDiskMgrException;
import heap.HFException;

import java.util.ArrayList;
import java.io.IOException;

import va.BitSet;
import va.Range;
import va.VAEntry;
import va_file.VAFile;
import va_file.VAFileScan;
import global.PageId;
import global.RID;
import global.SystemDefs;
import global.Vector100Dtype;

public class VAScanTest {
	
	public static void main(String[] args) throws HFException, HFBufMgrException,
	HFDiskMgrException, IOException
	{
		
		String dbPath = "/tmp/Shishir.minibase-db";
		SystemDefs sysdef = new SystemDefs(dbPath,100,100,"Clock");
		
		VAFile file = new VAFile("testFile4");
		VAScanTestOperations vaOperation=new VAScanTestOperations();
		vaOperation.insertRecordsIntoVAFile(file);
		Vector100Dtype vdTarget=new Vector100Dtype();
		int i=0;
		RID tempRID=new RID();
		try {
			VAFileScan vaFileScan = file.openVAScan();
			ArrayList<RID> rid= vaFileScan.VAFileRangeScan(vdTarget, 1000,4);
			//ArrayList<RID> rid=vaFileScan.VAFileNNScan(vdTarget,2,4);
			for(i=0;i<rid.size();i++)
			{
				tempRID=rid.get(i);
				System.out.println("RID is "+tempRID.slotNo+"."+tempRID.pageNo.pid);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}

class VAScanTestOperations
{
	private static int pageNo=0;	
	private static int slotNo=0;	
	
	public VAEntry createVAEntry(short val)
	{
		Vector100Dtype vd=new Vector100Dtype();
		BitSet vectorApproximation=new BitSet();
		//Random generator=new Random();
		int i=0;
		for(i=0;i<100;i++)
		{
			//val=(short)generator.nextInt(101);
			vd.setIndex(i,val);
		}
		Range ranges[]=VAFile.GetDefaultRanges();
		vectorApproximation=VAEntry.CreateApproximation(vd, ranges, 4);	
		PageId pid=new PageId(pageNo);
		System.out.println("PageNo"+pageNo);
		RID rid=new RID(pid,slotNo);
		System.out.println("slotNo"+slotNo);
		VAEntry entry=new VAEntry(vectorApproximation,rid);
		pageNo++;
		slotNo++;
		return entry;

	}
	
	public void insertRecordsIntoVAFile(VAFile file) throws HFException, HFBufMgrException,
	HFDiskMgrException, IOException
	{
		try
		{		
		short i=0;
		for(i=500;i<6000;i+=1250)
		{
		VAEntry entry=createVAEntry(i);
		System.out.println(entry.toString());
		file.insertRecord(entry);
		System.out.println("Record inserted successfully into Heap file");
		}
		}
		catch (Exception e ) { 
			e.printStackTrace();
		}
	}
}
